package com.xdf.whiteaccount.service.impl;

import com.xdf.whiteaccount.service.GjrkbilldraftCpDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import org.springframework.context.annotation.Lazy;
import com.xdf.whiteaccount.dao.GjrkbilldraftCpDetailMapper;
import com.xdf.whiteaccount.entity.GjrkbilldraftCpDetail;
import com.xdf.whiteaccount.utils.BusinessException;
import com.xdf.whiteaccount.utils.Example;
import com.xdf.whiteaccount.utils.BeanUtils;
import com.xdf.whiteaccount.enums.OpeEnum;
import com.xdf.whiteaccount.enums.MoveBillFlag;

/**
 * 版本号：20240401
 * @Description : 服务层实现类，由代码生成器自动生成
 * @Author : chanchaw
 * @Date : 2025-8-2 9:55:00
 */
@Slf4j
@Service
public class GjrkbilldraftCpDetailServiceImpl implements GjrkbilldraftCpDetailService {
    @Autowired
    private GjrkbilldraftCpDetailMapper dao;

    @Lazy
    @Autowired
    private GjrkbilldraftCpDetailServiceImpl meService;

    @Override
    @Transactional
    public GjrkbilldraftCpDetail insert(GjrkbilldraftCpDetail record) {
        record = meService.fullFill(record);
        meService.doBeforeInsert(record);
        dao.insert(record);
        meService.doAfterInsert(record);
        return record;
    }

    @Override
    @Transactional
    public GjrkbilldraftCpDetail insertSelective(GjrkbilldraftCpDetail record) {
        record = meService.fullFill(record);
        meService.doBeforeInsert(record);
        dao.insertSelective(record);
        meService.doAfterInsert(record);
        return record;
    }

    /* 由于本方法无法返回所有对象的自增主键，后者调整为一个一个保存的方法
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<GjrkbilldraftCpDetail> list) {
        return dao.multiInsert(list);
    }
     */
    @Override
    @Transactional
    public List<GjrkbilldraftCpDetail> multiInsert(List<GjrkbilldraftCpDetail> list) {
        list.forEach(item -> meService.insertSelective(item));
        return list;
    }

    @Override
    public int updateByPrimaryKey(GjrkbilldraftCpDetail record) {
        try {
            BeanUtils.fullFillRecord(record, OpeEnum.UPDATE);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(GjrkbilldraftCpDetail record) {
        try {
            BeanUtils.fullFillRecord(record, OpeEnum.UPDATE);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        GjrkbilldraftCpDetail dbRecord = dao.selectByPrimaryKey(id);
        if( dbRecord == null ) return 0;
        return dao.updateByPrimaryKey(dbRecord);
    }

    @Override
    public GjrkbilldraftCpDetail selectByPrimaryKey(Integer id) {
        return dao.selectByPrimaryKey(id);
    }

    // 根据主键作废
    @Override
    public int setVoid(Integer id){
        GjrkbilldraftCpDetail dbRecord = dao.selectByPrimaryKey(id);
        if( dbRecord == null ) return 0;
        return dao.updateByPrimaryKey(dbRecord);
    }

    @Override
    public List<GjrkbilldraftCpDetail> selectAll(){
        return dao.selectAll();
    }

    @Override
    public GjrkbilldraftCpDetail save(GjrkbilldraftCpDetail record){
        if(record == null) throw new BusinessException("空对象不可保存！");
        Integer iid = Optional.ofNullable(record.getId()).orElse(0);
        if(iid==0) record = insertSelective(record);
        else updateByPrimaryKeySelective(record);
        return record;
    }

    // 没有使用 xml 中的 multiSave 方法是因为无法批量回写自增主键
    @Transactional
    @Override
    public List<GjrkbilldraftCpDetail> multiSave(List<GjrkbilldraftCpDetail> list){
        if(list == null || list.size()==0) return list;
        list.forEach(item -> item = meService.save(item));
        return list;
    }

    /**=========================下面开始是固定模式代码=======================================*/
    // 新增前检测，不可新增则抛出异常
    private void check4Insert(GjrkbilldraftCpDetail record){
    }

    @Override
    public GjrkbilldraftCpDetail fullFill(GjrkbilldraftCpDetail record) {
        // TODO: 默认前端传递来的参数不需要填充完整，如果需要则使用自定义逻辑覆盖下面的代码
        try {
            // 根据“新增”或者“修改”填充创建人或者最后一次修改人
            // 要求表格中有字段：create_user,last_user
            BeanUtils.fullFillRecord(record,OpeEnum.NEW);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return record;
    }

    @Override
    public void doBeforeInsert(GjrkbilldraftCpDetail record) {
        // TODO: 在新增数据前需要做的逻辑写在这里，抛出异常则终止新增操作
        GjrkbilldraftCpDetailService.super.doBeforeInsert(record);
    }

    @Override
    public void doAfterInsert(GjrkbilldraftCpDetail record) {
        // TODO: 新增数据后需要做的逻辑写在这里，抛出异常则终止新增操作
        GjrkbilldraftCpDetailService.super.doAfterInsert(record);
    }

    @Override
    public List<GjrkbilldraftCpDetail> selectByExample(Example<GjrkbilldraftCpDetail> example){
        return dao.selectByExample(example);
    }

    @Override
    public GjrkbilldraftCpDetail moveBill(Integer id, String moveBillFlag){
        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.FIRST.getFlag())){
            GjrkbilldraftCpDetail first = dao.getFirst();
            if(first == null) throw new BusinessException("没有单据！");
            else return first;
        }

        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.LAST.getFlag())){
            GjrkbilldraftCpDetail last = dao.getLast();
            if(last == null) throw new BusinessException("没有单据！");
            else return last;
        }
        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.NEXT.getFlag())){
            GjrkbilldraftCpDetail next = dao.getNext(id);
            if(next == null) throw new BusinessException("没有下一单了！");
            else return next;
        }

        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.PREVIOUS.getFlag())){
            GjrkbilldraftCpDetail prev = dao.getPrev(id);
            if(prev == null) throw new BusinessException("没有上一单了！");
            else return prev;
        }

        return null;
        }
    //=================以上是自动生成的代码，自定义代码请写在下面==================
}